package com.tjsinfo.tjsoa.bylaws.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;

import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.bylaws.dao.IBylawsDao;
import com.tjsinfo.tjsoa.bylaws.vo.AbstractBylaws;
import com.tjsinfo.tjsoa.common.PaginationSupport;
import com.tjsinfo.tjsoa.system.vo.User;
import com.tjsinfo.tjsoa.workplan.bo.IWorkPlanMainBo;

public class BylawsDeleteAction extends Action{
	private IBylawsDao bylawsDao;
	
	public void setBylawsDao(IBylawsDao bylawsDao){
		this.bylawsDao=bylawsDao;
	}
	private IWorkPlanMainBo workPlanMainBo;
	public IWorkPlanMainBo getWorkPlanMainBo() {
		return workPlanMainBo;
	}

	public void setWorkPlanMainBo(IWorkPlanMainBo workPlanMainBo) {
		this.workPlanMainBo = workPlanMainBo;
	}
	private IQbcDao qbcDao; 
	
	public IQbcDao getQbcDao() {
		return qbcDao;
	}

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}
	public ActionForward execute(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		try{
			HttpSession session=request.getSession();
			User user = (User)session.getAttribute("user");
			String cid = request.getParameter("cid");
			String articleId = request.getParameter("id");
			String auditing_sign = request.getParameter("auditing_sign");
			//如果审核则提示已审核,如没审核则删除

			if (auditing_sign == "1"){
				return mapping.findForward("yishenhe");
			}else{
			
				bylawsDao.removeArticle(articleId);
			}
			List result = null;
			//翻页
			DetachedCriteria criteria = DetachedCriteria.forClass(AbstractBylaws.class);
			criteria.add(Expression.and(Expression.eq("cid", cid), Expression.eq("userId",user.getUsername())));
			Integer pageSize=15;
			String pageStr=request.getParameter("pageNo");
			if(pageStr==null)pageStr="0";
			int page=Integer.valueOf(pageStr).intValue();
			int startIndex=pageSize*page;
			List<Order> orderList=new ArrayList<Order>();
			orderList.add(Order.desc("id"));
			PaginationSupport paginationSupport = null;
			try {
				paginationSupport=qbcDao.findPageByCriteria(criteria,orderList, Integer.valueOf(pageSize),startIndex);
			} catch (Exception e) {
				e.printStackTrace();
				// TODO: handle exception
			}
			
			request.setAttribute("result", paginationSupport);
			request.setAttribute("intPageSize", pageSize);
			request.setAttribute("cid", cid);
			request.setAttribute("crudMethod", "crudMethod");
			workPlanMainBo.selectMapWorkPlanMain(request);
			return mapping.findForward("success");
		}catch(Exception e){
			return mapping.findForward("failed");
		}
	}

}
